package com.coocaa.app.lib.cleaner.scanner.config;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lu on 17-4-17.
 */

public class ConfigDataTest {
    @Before
    public void setUp() throws Exception {

    }

    @Test
    public void testKeep() throws Exception {
        ConfigDataFactory.ConfigData data = ConfigDataFactory.factory.create();
        String packageName = "aa.bb.cc";
        String file = "file:";
        for (int i = 0; i < 10; i++)
            data.appendPackageKeep(packageName, file + i);
        for (int i = 0; i < 5; i++)
            data.appendFileKeep(file + i);
        for (int i = 0; i < 10; i++) {
            Assert.assertTrue(data.containsPackageKeep(packageName, file + i));
            Assert.assertFalse(data.containsPackageKeep(packageName + i, file + i));
            if (i < 5)
                Assert.assertTrue(data.containsFileKeep(file + i));
            else
                Assert.assertFalse(data.containsFileKeep(file + i));
        }
        Assert.assertFalse(data.containsPackageKeep(packageName, file + 100));
        Assert.assertFalse(data.containsFileKeep(file + 100));
    }

    @Test
    public void testDel() throws Exception {
        ConfigDataFactory.ConfigData data = ConfigDataFactory.factory.create();
        String packageName = "aa.bb.cc";
        String file = "file:";
        List<String> packageDels = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            data.appendPackageDel(packageName, file + i);
            packageDels.add(file + i);
        }

        List<String> fileDels = new ArrayList<String>();
        for (int i = 0; i < 5; i++) {
            data.appendFileDel(file + i);
            fileDels.add(file + i);
        }
        List<String> _packageDels = data.getPackageDelFiles(packageName);
        Assert.assertEquals(packageDels, _packageDels);


        List<String> _fileDels = data.getFileDelFiles();
        Assert.assertEquals(fileDels, _fileDels);
    }

    @Test
    public void testAppendAll() throws Exception {
        ConfigDataFactory.ConfigData data1 = ConfigDataFactory.factory.create();
        String packageName1 = "packageName1";
        String file1 = "file1:";
        String file1del = "file1del:";
        for (int i = 0; i < 10; i++) {
            data1.appendPackageKeep(packageName1, file1 + i);
            data1.appendFileKeep(file1 + i);
            data1.appendPackageDel(packageName1, file1del + i);
            data1.appendFileDel(file1del + i);
        }


        ConfigDataFactory.ConfigData data2 = ConfigDataFactory.factory.create();
        String packageName2 = "packageName2";
        String file2 = "file2:";
        String file2del = "file2del:";
        for (int i = 0; i < 10; i++) {
            data2.appendPackageKeep(packageName2, file2 + i);
            data2.appendFileKeep(file2 + i);
            data2.appendPackageDel(packageName2, file2del + i);
            data2.appendFileDel(file2del + i);
        }


        ConfigDataFactory.ConfigData data3 = data1.appendAll(data2);
        for (int i = 0; i < 10; i++) {
            Assert.assertTrue(data3.containsPackageKeep(packageName1, file1 + i));
            Assert.assertTrue(data3.containsPackageKeep(packageName2, file2 + i));
            Assert.assertTrue(data3.containsFileKeep(file1 + i));
            Assert.assertTrue(data3.containsFileKeep(file2 + i));
            Assert.assertTrue(data3.containsPackageDel(packageName1, file1del + i));
            Assert.assertTrue(data3.containsPackageDel(packageName2, file2del + i));
            Assert.assertTrue(data3.containsFileDel(file1del + i));
            Assert.assertTrue(data3.containsFileDel(file2del + i));
        }
    }
}
